package cn.waka.sys.admin.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.waka.sys.admin.dao.BannerDao;
import cn.waka.sys.admin.object.Banner;
import cn.waka.sys.admin.object.Column;

@Service
public class BannerService {
    private BannerDao dao;

    @Autowired
    public void setDao(BannerDao dao) {
        this.dao = dao;
    }

    public int saveBanner(Banner banner) {
        int rs = 0;
        if (banner.getId() > 0) {
            rs = dao.update(banner);
        } else {
            rs = dao.save(banner);
        }
        return rs;
    }

    public List<Map<String, Object>> getBannerList() {
        return dao.getBannerList();
    }

    public Banner getBannerById(int id) {
        return dao.getBannerById(id);
    }

    public List<Map<String, Object>> getColumnList() {
        return dao.getColumnList();
    }

    public int isShow(int id, int status) {
        return dao.isShow(id, status);
    }

    public int showOrHidden(int id, int status) {
        return dao.showOrHidden(id, status);
    }

    public int top_banner(int id, int sort) {
        return dao.top_banner(id, sort);
    }

    public Column getColumnById(int id) {
        return dao.getColumnById(id);
    }

    public int sortBanner(Banner banner, String action) {

        int a = 0;
        int maxSort = dao.maxBannerSort();
        System.out.println(maxSort + ":" + banner);
        if (action.equals("top") && banner.getSort() < maxSort) {
            a = dao.top_banner(banner.getId(), maxSort);
            dao.top_cut(banner.getSort());
        } else if (action.equals("up") && banner.getSort() < maxSort) {
            a = dao.up_cut(banner.getSort());
            dao.up_banner(banner.getId(), banner.getSort());
        } else if (action.equals("down") && banner.getSort() > 1) {
            a = dao.down_cut(banner.getSort());
            dao.down_banner(banner.getId(), banner.getSort());
        }
        return a;
    }

    public int sortColumn(Column column, String action) {

        int a = 0;
        int maxSort = dao.maxSort();
        if (action.equals("top") && column.getSort() < maxSort) {
            a = dao.top_column(column.getId(), maxSort);
            dao.top_del(column.getSort());
        } else if (action.equals("up") && column.getSort() < maxSort) {
            a = dao.up_del(column.getSort());
            dao.up_column(column.getId(), column.getSort());
        } else if (action.equals("down") && column.getSort() > 1) {
            a = dao.down_del(column.getSort());
            dao.down_column(column.getId(), column.getSort());
        }
        return a;
    }
}
